package com.axjy.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by asus on 2017/9/8.
 */
public class Tools {

    public static String han2pin(char ch){
        HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String[] arr=null;
        try {
           arr= PinyinHelper.toHanyuPinyinStringArray(ch, format);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return arr[0];
    }

    public static String han2pin(String str){
        StringBuffer sbf=new StringBuffer();
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            sbf.append(han2pin(ch));

        }
        return sbf.toString();
    }
}
